package org.lcc.Controller;

import org.lcc.Service.NotificationService;
import org.lcc.model.VO.NotificationVO;
import org.lcc.model.dto.NotificationDto;
import org.lcc.model.entity.BusinessException;
import org.lcc.util.ApiResponseMe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/notification")
public class NotificationController {

    @Autowired
    private NotificationService notificationService;

    /**
     * 发送通知
     */
    @PostMapping("/send")
    public ApiResponseMe sendNotification(@RequestBody NotificationDto notificationDto,
                                          @RequestParam String senderId) {
        // 参数校验
        if (notificationDto.getTitle() == null || notificationDto.getTitle().isEmpty()) {
            throw new BusinessException(400, "通知标题不能为空");
        }
        if (notificationDto.getContent() == null || notificationDto.getContent().isEmpty()) {
            throw new BusinessException(400, "通知内容不能为空");
        }
        
        // 设置默认通知类型为班级
        if (notificationDto.getNotificationType() == null || notificationDto.getNotificationType().isEmpty()) {
            notificationDto.setNotificationType("class");
        }
        
        // 类型校验
        if (!notificationDto.getNotificationType().equals("student") &&
            !notificationDto.getNotificationType().equals("class")) {
            throw new BusinessException(400, "通知类型无效，只能是class或student");
        }
        
        // 目标校验
        if (notificationDto.getTargetIds() == null || notificationDto.getTargetIds().isEmpty()) {
            throw new BusinessException(400, "通知目标不能为空");
        }
        
        Map<String, Object> result = notificationService.sendNotification(notificationDto, senderId);
        return ApiResponseMe.success(result);
    }

    /**
     * 获取所有通知（管理员使用）
     */
    @GetMapping("/all")
    public ApiResponseMe getAllNotifications(@RequestParam(defaultValue = "1") int pageNum,
                                             @RequestParam(defaultValue = "10") int pageSize) {
        Map<String, Object> result = notificationService.getAllNotifications(pageNum, pageSize);
        return ApiResponseMe.success(result);
    }

    /**
     * 获取学生的通知列表
     */
    @GetMapping("/student/{studentId}")
    public ApiResponseMe getStudentNotifications(@PathVariable String studentId,
                                                 @RequestParam(defaultValue = "1") int pageNum,
                                                 @RequestParam(defaultValue = "10") int pageSize) {
        Map<String, Object> result = notificationService.getStudentNotifications(studentId, pageNum, pageSize);
        return ApiResponseMe.success(result);
    }

    /**
     * 获取教师发送的通知列表
     */
    @GetMapping("/teacher/{teacherId}")
    public ApiResponseMe getTeacherNotifications(@PathVariable String teacherId,
                                                 @RequestParam(defaultValue = "1") int pageNum,
                                                 @RequestParam(defaultValue = "10") int pageSize) {
        Map<String, Object> result = notificationService.getTeacherNotifications(teacherId, pageNum, pageSize);
        return ApiResponseMe.success(result);
    }

    /**
     * 获取通知详情
     */
    @GetMapping("/{notificationId}")
    public ApiResponseMe getNotificationDetail(@PathVariable Integer notificationId,
                                               @RequestParam String studentId) {
        NotificationVO notification = notificationService.getNotificationDetail(notificationId, studentId);
        
        if (notification != null) {
            return ApiResponseMe.success(notification);
        } else {
            return ApiResponseMe.error(404, "通知不存在或无权查看");
        }
    }

    /**
     * 标记通知为已读
     */
    @PutMapping("/{notificationId}/read")
    public ApiResponseMe markAsRead(@PathVariable Integer notificationId,
                                    @RequestParam String studentId) {
        boolean success = notificationService.markAsRead(notificationId, studentId);
        
        if (success) {
            return ApiResponseMe.shape(true, "标记成功");
        } else {
            return ApiResponseMe.shape(false, "标记失败，通知不存在或无权操作");
        }
    }

    /**
     * 获取学生未读通知数量
     */
    @GetMapping("/unread/count")
    public ApiResponseMe countUnreadNotifications(@RequestParam String studentId) {
        int count = notificationService.countUnreadNotifications(studentId);
        
        Map<String, Object> result = new HashMap<>();
        result.put("count", count);
        
        return ApiResponseMe.success(result);
    }

    /**
     * 删除通知（管理员或发送者）
     */
    @DeleteMapping("/{notificationId}")
    public ApiResponseMe deleteNotification(@PathVariable Integer notificationId) {
        boolean success = notificationService.deleteNotification(notificationId);
        
        if (success) {
            return ApiResponseMe.shape(true, "删除成功");
        } else {
            return ApiResponseMe.shape(false, "删除失败，通知不存在或无权操作");
        }
    }
} 